home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group99a.txt / 000219_icon-group-sender _Thu Oct 21 15:43:50 1999.msg < prev    next >
Internet Message Format  |  2000-09-20  |  3KB

  1. Return-Path: <icon-group-sender>
  2. Received: (from root@localhost)
  3.     by baskerville.CS.Arizona.EDU (8.9.1a/8.9.1) id PAA05509
  4.     for icon-group-addresses; Thu, 21 Oct 1999 15:42:36 -0700 (MST)
  5. Message-Id: <199910212242.PAA05509@baskerville.CS.Arizona.EDU>
  6. Date: Thu, 21 Oct 1999 09:01:50 -0700
  7. From: Steve Wampler <swampler@noao.edu>
  8. X-Accept-Language: en
  9. CC: icon-group@optima.CS.Arizona.EDU
  10. Subject: Re: List subscription insert/removal
  11. Errors-To: icon-group-errors@optima.CS.Arizona.EDU
  12. Status: RO
  13.  
  14. Blake Chapman wrote:
  15. > Icon Project,
  16. > I am trying to figure out how to insert a new element into a list,
  17. > and remove an element from the middle of a list (not the end).
  18. > In answer to this question previously Gordon Peterson sent
  19. > a reply.  I tried what he said and it doesn't work.
  20. > My original question, that he answered, was
  21. > "I find no routines to pluck one element out of the middle of a list, or insert
  22. > one in.  Are there such routines?".
  23. ...
  24. > procedure main()
  25. >  local s  # it will be a list
  26. >  s:= ["a", "b", "c", "d"]
  27. >  WriteL(s)   # this writes "abcd" as it should
  28. >   # Next, I want to insert "x" as a list element between "b" and "c" ....
  29. >   # didn't work:  s[3:3]:="x"
  30. >   # didn't work:  s[3:3] := ["x"]
  31. >   # didn't work:  s[3+:0] := ["x"]
  32. >   s[3+:0] := "x"   # didn't work; I get a run-time error and program abort
  33. >  WriteL(s)
  34. > end
  35.  
  36. Unfortunately, sublists are not variables, as you've seen with your test
  37. cases.  So trying to assign to a sublist isn't going to work, no matter
  38. how you try and construct it.  I'm not sure why sublists are implemented
  39. this way, but can imagine all sorts of difficulties that might arise from
  40. implementing it otherwise.  (I've often thought that it would be nice
  41. to implement lists so they are more consistent with strings, but the result,
  42. while perhaps quite nice, would no longer be Icon...  Perhaps someone with
  43. more time on their hands would like to think about this more?  (List scanning
  44. would become easier to implement, among other things.)
  45.  
  46. Anyway, you have to implement the operations you want by constructing
  47. new lists:
  48.  
  49. procedure replace(a, start, stop, value)
  50.     return a[1:start] ||| value ||| a[stop:0]
  51. end
  52.  
  53. where the call would be:
  54.  
  55.     s := replace(s, 3, 3, ["x"])
  56.  
  57. I agree that this is not as clean as one might hope for, but I don't
  58. think there's any way around this.  
  59.  
  60. Because of the call-by-value semantics of Icon, you have
  61. to do the "pluck" (where I assume you mean to remove the elements
  62. from the list when you pluck them) as a two step process:
  63.  
  64.     a := s[3:5]
  65.     s := replace(s, 3, 5, [])
  66.  
  67. --
  68. Steve Wampler-  SOLIS Project, National Solar Observatory
  69. swampler@noao.edu
  70.